<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-11-26
 * Time: 9:45
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use app\common\model\Messagem;

class Message extends AdminBase
{

    /**
     * 消息模块列表
     */
    public function index($keyword = '', $page = 1)
    {
        $map = [];
        if ($keyword) {
            $map['mm_title'] = ['like', "%{$keyword}%"];
        }
        $list = Messagem::messageList($map, $page);
        return $this->fetch('index', ['list' => $list, 'keyword' => $keyword]);
    }

    /**
     * 添加消息模块页面
     */
    public function add()
    {
        return $this->fetch();
    }
    /**
     * 添加消息模块
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $status = Messagem::messageSave($data);
            if ($status) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /**
     * 消息模块详情
     */
    public function detail($id)
    {
        $detail = Messagem::detail($id);
        return $this->fetch('detail', ['detail' => $detail]);
    }

    /**
     * 修改消息模块
     * @param $id
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $data['mm_update_time'] = time();
            $status = Messagem::updateType($data);
            if ($status) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }

    /**
     * 禁用启用消息模块
     */
    public function editstatus($id, $status)
    {
        if ($status == 1) {
            $status = 2;
        } else if ($status == 2) {
            $status = 1;
        } else {
            $this->error('修改失败');
        }
        $status = Messagem::editStatus($id, $status);
        if ($status) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    /**
     * 用户消息列表
     */
    public function userindex($keyword = '', $page = 1)
    {
        $map = [];
        if ($keyword) {
            $map['u_phone|u_email|u_nickname'] = trim($keyword);
        }
        $list = Messagem::userMessageList($map, $page);
        return $this->fetch('userindex', ['list' => $list, 'keyword' => $keyword]);
    }

    /**
     * 用户消息详情
     */
    public function userdetail($id)
    {
        $detail = Messagem::userMessageInfo($id);
        return $this->fetch('userdetail', ['detail' => $detail]);
    }






}